package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.IUserAuthService;
import com.kk.xhr.admin.biz.IUserBizService;
import com.kk.xhr.admin.controller.request.PasswordModifyRequest;
import com.kk.xhr.admin.controller.request.UserLoginRequest;
import com.kk.xhr.admin.controller.vo.AuthTokenVO;
import com.kk.xhr.admin.controller.vo.UserVO;
import com.kk.xhr.common.ErrorCodeEnum;
import com.kk.xhr.common.pojo.AuthUser;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 用户认证控制器
 *
 * @author kkmystery
 * @version 1.0 2021/3/11
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "认证管理", tags = "认证管理")
@RequestMapping("auth")
public class UserAuthController {
    private final Executor adminControllerBizExecutor;
    private final IUserAuthService userAuthService;
    private final IUserBizService userBizService;

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public CompletionStage<ApiResult<AuthTokenVO>> login(@RequestBody @Valid UserLoginRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            Pair<ErrorCodeEnum, AuthTokenVO> tokenInfoPair = userAuthService.login(request);
            // 记录
            if (ErrorCodeEnum.OK.equals(tokenInfoPair.getLeft())) {
                return ApiResult.success(tokenInfoPair.getRight());
            } else {
                return ApiResult.failed(tokenInfoPair.getLeft());
            }
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("info")
    public CompletionStage<ApiResult<UserVO>> queryInfo() {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(userBizService.getUserInfo(userId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public CompletionStage<ApiResult<Void>> logout() {
        return CompletableFuture.supplyAsync(() -> {
            AuthUser user = (AuthUser) SecurityUtils.getSubject().getPrincipal();
            SecurityUtils.getSubject().logout();
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/password")
    public CompletionStage<ApiResult<Void>> modifyPassword(@Valid PasswordModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            Long userId = ShiroUtil.getAuthUserId();
           userAuthService.modifyPassword(userId, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}


